#ifndef HEAD_H
#define HEAD_H
#include<apue.h>
#include <mysql/mysql.h>

#define STAFF_DB "user_message_db"

#define PORT_COUNT 3

#define THREAD_NUMBER 300

typedef enum
{
	C2S_REGISTER=1,
	C2S_LOGIN,
	C2S_RECHARGE
}operate_t;

struct user_info_t
{
	char user_name[20];
	char passwd[20];
};

struct register_t
{
	struct user_info_t info;
};

struct login_t
{
	struct user_info_t info;
};

struct recharge_t
{
	char charge_pass[20];
	struct user_info_t info;
};

typedef struct requst_t
{
	operate_t op;
	union
	{
		struct register_t reg_msg;
		struct login_t log_msg;
		struct recharge_t rec_msg;
	}oparg;
}c2s_t;

struct thread_oparg
{
	char buf[128];
	int op;
	c2s_t requst;
	int sock_fd;
	struct sockaddr_in peer_addr;
};
void* c2s_dealwith_func(void*arg);
void register_func(struct thread_oparg msg);
void login_func   (struct thread_oparg msg);
void recharge_func(struct thread_oparg msg);

typedef enum 
{
	REGISTER_FAIL,REGISTER_SUCC
}s2c_register_t;

typedef enum 
{
	USER_NAME_UNEXIST,CARD_ID_UNSAME,LOGIN_SUCC
}s2c_login_t;

struct s2c_login_message_t
{
	int charge;
	s2c_login_t msg_back;
};

typedef enum 
{
	RECHARGE_FAIL,RECHARGE_SUCC
}s2c_recharge_t;


typedef struct queue
{
	struct thread_oparg data;
	struct queue* next,*prev;
}sQueue,*pQueue;

typedef struct
{
	int tp_num;
	pthread_t *tp_tid;
	void *(*tp_func)(void *);
	void *tp_arg;
	pQueue tp_pq;
	pthread_mutex_t tp_mutex;
	pthread_cond_t tp_cond;
}ThreadPool_t;

ThreadPool_t * initThreadPool(int num,void *(*func)(void*),void *a);
void runThreadPool(ThreadPool_t *tp);
void destroyThreadPool(ThreadPool_t *tp);
void addThreadPool(ThreadPool_t *tp,struct thread_oparg data);
void getThreadPool(ThreadPool_t *tp,struct thread_oparg *data);
void setNonBlock(int fd);
void print_err (MYSQL* my_sql, char * message);

typedef struct user_online
{
	int sock_fd;	
	char user_name[20];
	struct sockaddr_in user_addr;
	struct user_online *next,*prev;
}sOl,*pOl;

void init_online(pOl* pnode);
void add_online_chain(pOl phead,pOl pnew);

#endif
